package com.e7.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.e7.common.Constants;
import com.e7.common.E7Action;
import com.e7.common.IModelObject;
import com.e7.common.IPersistenceManager;
import com.e7.common.PersistenceManagerAware;
import com.e7.service.ValidateService;
import com.opensymphony.xwork2.Action;

@Component("validaterAction")
public class ValidateAction extends E7Action implements PersistenceManagerAware {

	private static final long serialVersionUID = 7413739975236562493L;
	private ValidateService validater = new ValidateService();
	private IPersistenceManager manager;
	// client set
	private String type;
	private String value;

	// return

	@Override
	public void validate() {
		if (this.validater.isEmpty(value)) {
			this.code = Constants.ErrorCode.EMPTY;
			this.msg = "it's empty";
			this.addActionError("empty");
		}
	}

	public boolean exists(IModelObject obj) {
		return this.manager.exists(obj) > 0;
	}

	public <T extends IModelObject> boolean exists(Class<T> clazz,
			String field, Object value) {
		List<T> objs = this.manager.find(clazz, field, value);
		return objs != null && objs.size() > 0;
	}

	public <T extends IModelObject> boolean exists(Class<T> clazz,
			List<String> fields, List<Object> values) {
		List<T> objs = this.manager.find(clazz, fields, values);
		return objs != null && objs.size() > 0;
	}

	@Override
	public String execute() {
		if (this.validater.isEmpty(type)) {
			this.code = Constants.ErrorCode.EMPTY;
			this.msg = "you should at least tell me the type of the value";
			return "fail";
		}
		if (type.equals("email")) {
			return this.validateEmail();
		} else if (type.equals("phone")) {
			return this.validatePhone();
		} else if (type.equals("name")) {
			return this.validateNickName();
		} else {
			return "error";
		}
	}

	public String validateEmail() {
		if (this.validater.validateEmail(this.value) > 0) {
			this.code = 1;
			this.msg = "OK";
			return Action.SUCCESS;
		}
		this.code = Constants.ErrorCode.INVALID;
		this.msg = "this field must be an email address.";
		return "fail";
	}

	public String validatePhone() {
		if (this.validater.validatePhone(this.value) > 0) {
			this.code = 1;
			this.msg = "OK";
			return Action.SUCCESS;
		}
		this.code = Constants.ErrorCode.INVALID;
		this.msg = "this field must be a phone number.";
		return "fail";
	}

	public String validateNickName() {
		if (this.validater.validateName(this.value) > 0) {
			this.code = 1;
			this.msg = "OK";
			return Action.SUCCESS;
		}
		this.code = Constants.ErrorCode.INVALID;
		this.msg = "only numbers and letters are accepted.";
		return "fail";
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Resource
	public void setValidater(ValidateService validater) {
		this.validater = validater;
	}

	@Resource
	@Override
	public void setPersistenceManager(IPersistenceManager manager) {
		this.manager = manager;
	}
}
